package cn.wolfcode.web.modules.customer.controller;

import cn.wolfcode.web.commons.entity.LayuiPage;
import cn.wolfcode.web.commons.utils.CityUtils;
import cn.wolfcode.web.commons.utils.LayuiTools;
import cn.wolfcode.web.commons.utils.SystemCheckUtils;
import cn.wolfcode.web.modules.BaseController;
import cn.wolfcode.web.modules.log.LogModules;

import cn.wolfcode.web.modules.customer.entity.Customer;
import cn.wolfcode.web.modules.customer.service.ICustomerService;

import link.ahsj.core.annotations.AddGroup;
import link.ahsj.core.annotations.SameUrlData;
import link.ahsj.core.annotations.SysLog;
import link.ahsj.core.annotations.UpdateGroup;
import link.ahsj.core.entitys.ApiModel;
import link.ahsj.core.entitys.KeyValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author tangzehua
 * @since 2022-06-20
 */
@Slf4j
@Controller
@RequestMapping("customer")
public class CustomerController extends BaseController {

    @Autowired
    private ICustomerService entityService;

    private static final String LogModule = "Customer";

    @GetMapping("/list.html")
    public String list() {
        return "customer/customer/list";
    }

    @RequestMapping("/add.html")
    @PreAuthorize("hasAuthority('customer:customer:add')")
    public ModelAndView toAdd(ModelAndView mv) {
        List<KeyValue> citys = CityUtils.citys;
        mv.addObject("citys", citys);
        mv.setViewName("customer/customer/add");
        return mv;
    }

    @GetMapping("/{id}.html")
    @PreAuthorize("hasAuthority('customer:customer:update')")
    public ModelAndView toUpdate(@PathVariable("id") String id, ModelAndView mv) {

        mv.addObject("citys",CityUtils.citys);
        mv.setViewName("customer/customer/update");
        mv.addObject("obj", entityService.getById(id));
        mv.addObject("id", id);
        return mv;
    }

    @RequestMapping("list")
    @PreAuthorize("hasAuthority('customer:customer:list')")
    public ResponseEntity page(LayuiPage layuiPage,
                               @RequestParam(defaultValue = "") String customerName){
        SystemCheckUtils.getInstance().checkMaxPage(layuiPage);
        return ResponseEntity.ok(LayuiTools.toLayuiTableModel(entityService.pageCustomer(layuiPage.getPage(),layuiPage.getLimit(),customerName)));
    }

    @SameUrlData
    @PostMapping("save")
    @SysLog(value = LogModules.SAVE, module =LogModule)
    @PreAuthorize("hasAuthority('customer:customer:add')")
    public ResponseEntity<ApiModel> save(@Validated({AddGroup.class}) @RequestBody Customer entity) {
        entityService.insert(entity);
        return ResponseEntity.ok(ApiModel.ok());
    }

    @SameUrlData
    @SysLog(value = LogModules.UPDATE, module = LogModule)
    @PutMapping("update")
    @PreAuthorize("hasAuthority('customer:customer:update')")
    public ResponseEntity<ApiModel> update(HttpServletResponse response, @Validated({UpdateGroup.class}) @RequestBody Customer entity) {

        entityService.updateMy(entity);
        return ResponseEntity.ok(ApiModel.ok());
    }

    @SysLog(value = LogModules.DELETE, module = LogModule)
    @DeleteMapping("delete/{id}")
    @PreAuthorize("hasAuthority('customer:customer:delete')")
    public ResponseEntity<ApiModel> delete(@PathVariable("id") String id) {
        entityService.removeById(id);
        return ResponseEntity.ok(ApiModel.ok());
    }

}
